package com.vcredit.creditcat.framework.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;

@SuppressWarnings("hiding")
public class ExportImport<T> {

	// 生成xlsx格式的调用方式 头部样式
	public static XSSFCellStyle createHeaderStyle07(XSSFWorkbook workbook) throws Exception {
		// 生成一个样式
		XSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		// 单元格颜色(颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字)
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		// 单元格填充的样式
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 单元格边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setWrapText(true);// 设置自动换行
		// 单元格对齐方式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		XSSFFont font = workbook.createFont();
		// 颜色
		font.setColor(HSSFColor.VIOLET.index);
		// 字体大小
		font.setFontHeightInPoints((short) 12);
		// 粗体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		return style;
	}

	// 生成xlsx格式的调用方式 内容样式
	public static XSSFCellStyle createCellStyle07(XSSFWorkbook workbook) throws Exception {
		// 生成一个样式
		XSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		// 单元格颜色(颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字)
		style.setFillForegroundColor(HSSFColor.WHITE.index);
		// 单元格填充的样式
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 单元格边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setWrapText(true);// 设置自动换行
		// 单元格对齐方式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		XSSFFont font = workbook.createFont();
		// 颜色
		// font.setColor(HSSFColor.VIOLET.index);
		// 字体大小
		font.setFontHeightInPoints((short) 10);
		// 粗体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		return style;
	}

	// 03 头部样式
	public static HSSFCellStyle createHeaderStyle(HSSFWorkbook workbook) throws Exception {
		// 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		// 单元格颜色(颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字)
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		// 单元格填充的样式
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 单元格边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setWrapText(true);// 设置自动换行
		// 单元格对齐方式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		// 颜色
		font.setColor(HSSFColor.VIOLET.index);
		// 字体大小
		font.setFontHeightInPoints((short) 12);
		// 粗体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		return style;
	}

	// 03内容样式
	public static HSSFCellStyle createCellStyle(HSSFWorkbook workbook) throws Exception {
		// 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		// 单元格颜色(颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字)
		style.setFillForegroundColor(HSSFColor.WHITE.index);
		// 单元格填充的样式
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 单元格边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setWrapText(true);// 设置自动换行
		// 单元格对齐方式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		// 颜色
		// font.setColor(HSSFColor.VIOLET.index);
		// 字体大小
		font.setFontHeightInPoints((short) 10);
		// 粗体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		return style;
	}

	// 03调用入口
	public void createSheet03(List<T> list, String title, String[] headers, OutputStream out) throws Exception {

		// 声明一个工作薄
		HSSFWorkbook workBook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workBook.createSheet(title);

		int index = 0;
		// 标题行
		HSSFRow row = sheet.createRow(index);
		if (headers != null) {
			for (int i = 0; i < headers.length; i++) {
				HSSFCell cell = row.createCell(i);
				cell.setCellStyle(createHeaderStyle(workBook));
				HSSFRichTextString text = new HSSFRichTextString(headers[i]);
				cell.setCellValue(text);
			}
		}

		Iterator<T> it = list.iterator();
		while (it.hasNext()) {
			index++;
			row = sheet.createRow(index);
			createRow03(row, it.next(), workBook);

		}

		try {
			workBook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// // xlsx 07 创建 入口
	/**
	 *
	 * @param title
	 * @param headers
	 * @param out
	 * @param enumOnValue
	 * @param list
	 * @throws Exception
	 */
	public void createSheet(String title, String headers[], OutputStream out, String enumOnValue, List<T>... list )
			throws Exception {

		List<T> list1 = new ArrayList<T>();
		@SuppressWarnings("unused")
		List<T> list2 = new ArrayList<T>();
		if (list.length > 1) {
			for (int i = 0; i < list.length; i++) {
				list1 = list[0];
				list2 = list[1];
			}
		} else {
			for (List<T> listpara : list) {
				list1 = listpara;
			}
		}
		// 声明一个工作薄
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 生成一个表格
		XSSFSheet sheet = workbook.createSheet(title);

		int index = 0;
		// 标题行
		XSSFRow row = sheet.createRow(index);

		if (headers != null) {
			for (int i = 0; i < headers.length; i++) {
				sheet.setColumnWidth(i, 10 * 256);
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(createHeaderStyle07(workbook));
				XSSFRichTextString text = new XSSFRichTextString(headers[i]);
				cell.setCellValue(text);
			}
		}

		/*
		 * Iterator<T> it = list.iterator(); while (it.hasNext()) { index++; row
		 * = sheet.createRow(index); createRow(row, it.next(), workbook);
		 *
		 * }
		 */
		for (int i = 0; i < list1.size(); i++) {

			row = sheet.createRow(i + 1);
			createRow(row, list1.get(i), workbook);
		}
		/*// 根据cretime 来分组
		if (enumOnValue.equals("channe")) {
			createSheetBytime(workbook, list2, title, headers);
		}*/

		// 根据cretime 来分组
		if (enumOnValue.equals(ConstantsUtil.channe)) {

			sheet = workbook.createSheet(ConstantsUtil.channelSheet);

			index = 0;
			row = sheet.createRow(index);
			headers=ConstantsUtil.channeHeaders;
			if (headers != null) {
				for (int i = 0; i < headers.length; i++) {
					sheet.setColumnWidth(i, 10 * 256);
					XSSFCell cell = row.createCell(i);
					cell.setCellStyle(createHeaderStyle07(workbook));
					XSSFRichTextString text = new XSSFRichTextString(headers[i]);
					cell.setCellValue(text);
				}
			}
			for (int i = 0; i < list2.size(); i++) {

				row = sheet.createRow(i + 1);
				createRow(row, list2.get(i), workbook);
			}
		}
		try {
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// xlsl 07 创建行
	public void createRow(XSSFRow row, T bean, XSSFWorkbook workbook) throws Exception {
		XSSFCell cell;
		// 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {

			Field field = fields[i];
			String fieldName = field.getName();
			String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

			try {
				Class<? extends Object> tCls = bean.getClass();
				Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
				Object value = getMethod.invoke(bean, new Object[] {});

				if (value != null) {
					cell = row.createCell(i);
					cell.setCellStyle(createCellStyle07(workbook));
					if (value instanceof Integer) {
						cell.setCellValue(value.toString());

					} else {
						cell.setCellValue(value.toString());
					}
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	/**
	 * 比较是否相同-------------------------------------------------------------------
	 * -----
	 *
	 * @param bean
	 * @param time
	 * @return
	 */
	public Map<String, Object> timePrepared(T bean, String time) {
		// 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
		Map<String, Object> maRest = new HashMap<String, Object>();
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {

			Field field = fields[i];
			String fieldName = field.getName();
			String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

			try {
				Class<? extends Object> tCls = bean.getClass();
				Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
				Object value = getMethod.invoke(bean, new Object[] {});
				if (value != null) {
					if (i == fields.length - 1) {
						maRest.put("value", value);
						if (time.equals(value)) {
							maRest.put("prepared", true);
							return maRest;
						} else {
							maRest.put("prepared", false);
							return maRest;
						}

					}
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return null;
	}

	/**
	 * 比较是否相同-------------------------------------------------------------------
	 * -----
	 *
	 * @param bean
	 * @param time
	 * @return
	 */
	public boolean timeReturn(T bean, String time) {
		// 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {

			Field field = fields[i];
			String fieldName = field.getName();
			String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

			try {
				Class<? extends Object> tCls = bean.getClass();
				Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
				Object value = getMethod.invoke(bean, new Object[] {});
				if (value != null) {
					if (time.equals(value)) {
						return true;
					}
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return false;
	}

	// // xlsx 07
	public void createSheetBytime(XSSFWorkbook workbook, List<T> list, String title, String[] headers)
			throws Exception {
		// 声明一个工作薄

		String time = "";
		Map<String, Object> maps = new HashMap<String, Object>();
		boolean prepared = false;
		XSSFSheet sheet = workbook.createSheet("每日数据");
		;
		XSSFRow row = null;
		int jj = 0;
		for (int i = 0; i < list.size(); i++) {
			jj++;
			if (time != null) {
				maps = timePrepared(list.get(i), time);
				if (!maps.isEmpty()) {
					prepared = (boolean) maps.get("prepared");
					time = String.valueOf(maps.get("value"));
				}
			}
			if (false == prepared) {
				jj = 1;
				// 生成一个表格

				int index = 0;
				// 标题行
				row = sheet.createRow(index);

				if (headers != null) {
					for (int j = 0; j < headers.length; j++) {
						sheet.setColumnWidth(j, 10 * 256);
						XSSFCell cell = row.createCell(j);
						cell.setCellStyle(createHeaderStyle07(workbook));
						XSSFRichTextString text = new XSSFRichTextString(headers[j]);
						cell.setCellValue(text);
					}
				}
			}

			/*
			 * Iterator<T> it = list.iterator(); while (it.hasNext()) { index++;
			 * row = sheet.createRow(index); createRow(row, it.next(),
			 * workbook);
			 * 
			 * }
			 */

			row = sheet.createRow(jj);
			createRowByTime(row, list.get(i), workbook, time);
		}
	}

	// xlsl 07 创建行
	public void createRowByTime(XSSFRow row, T bean, XSSFWorkbook workbook, String time) throws Exception {

		XSSFCell cell;
		// 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {

			Field field = fields[i];
			String fieldName = field.getName();
			String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

			try {
				Class<? extends Object> tCls = bean.getClass();
				Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
				Object value = getMethod.invoke(bean, new Object[] {});
				if (i == fields.length - 1 && StringUtils.isBlank(time)) {
					time = value.toString();
				}
				if (value != null) {
					cell = row.createCell(i);
					cell.setCellStyle(createCellStyle07(workbook));
					if (value instanceof Integer) {
						cell.setCellValue(value.toString());

					} else {
						cell.setCellValue(value.toString());
					}
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	// ------------------------------------------------------------------------------------------------------------
	// // xls 03 创建行
	public void createRow03(HSSFRow row, T bean, HSSFWorkbook workbook) throws Exception {
		HSSFCell cell;
		// 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {

			Field field = fields[i];
			String fieldName = field.getName();
			String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

			try {
				Class<? extends Object> tCls = bean.getClass();
				Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
				Object value = getMethod.invoke(bean, new Object[] {});

				if (value != null) {
					cell = row.createCell(i);
					cell.setCellStyle(createCellStyle(workbook));
					if (value instanceof Integer) {
						cell.setCellValue(value.toString());
					} else {
						cell.setCellValue(value.toString());
					}
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public void downExcel(List<T> list, HttpServletResponse response) throws Exception {
		try {
			String path = "";
			OutputStream out = new FileOutputStream(path);
			createSheet("sheet1", null, out, "", list);
			out.flush();
			out.close();
			download(path, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void downExcel(List<T> list, String title, HttpServletResponse response) throws Exception {
		try {
			String path = "";
			OutputStream out = new FileOutputStream(path);
			createSheet("sheet1", null, out, "", list);
			out.flush();
			out.close();
			download(path, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void downExcel(List<T> list, String[] headers, HttpServletResponse response) throws Exception {
		try {
			String path = "";
			OutputStream out = new FileOutputStream(path);
			createSheet("sheet1", null, out, "", list);
			out.flush();
			out.close();
			download(path, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void downExcel(List<T> list, String[] headers, String title, HttpServletResponse response) throws Exception {
		try {
			String path = "";
			OutputStream out = new FileOutputStream(path);
			createSheet("sheet1", null, out, "", list);
			out.flush();
			out.close();
			download(path, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void downExcel(List<T> list, String[] headers, String title, String pattern, HttpServletResponse response,
						  String path) throws Exception {
		try {
			OutputStream out = new FileOutputStream(path);
			createSheet("sheet1", null, out, "", list);
			out.flush();
			out.close();
			System.out.println(path);
			download(path, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void download(String path, HttpServletResponse response) throws Exception {
		try {
			// path是指欲下载的文件的路径。
			File file = new File(path);
			// 取得文件名。
			String filename = file.getName();
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(filename.getBytes("ISO8859-1"), "utf-8"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void downloads(String path, HttpServletResponse response) throws IOException {
		// path是指欲下载的文件的路径。
		File file = new File(path);
		// 取得文件名。
		String filename = file.getName();
		// 取得文件的后缀名。
		// String ext = filename.substring(filename.lastIndexOf(".") +
		// 1).toUpperCase();

		// 以流的形式下载文件。
		InputStream fis = new BufferedInputStream(new FileInputStream(path));
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		fis.close();
		// 清空response
		response.reset();
		// 设置response的Header
		response.setHeader("Accept-Ranges", "bytes");
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(filename.getBytes(), "ISO8859-1"));
		response.addHeader("Content-Length", "" + file.length());
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream;charset=utf-8");
		response.setCharacterEncoding("UTF-8");// 设置Response的编码方式为UTF-8
		/*
		 * PrintWriter writer = response.getWriter();
		 * 
		 * writer.write("中国");
		 */
		toClient.write(buffer);
		toClient.flush();
		toClient.close();
	}

	public static void downloadFile(HttpServletRequest request, URL theURL, String filePath) throws IOException {
		File dirFile = new File(filePath);
		if (!dirFile.exists()) {// 文件路径不存在时，自动创建目录
			dirFile.mkdir();
		}
		// 从服务器上获取图片并保存
		URLConnection connection = theURL.openConnection();
		InputStream in = connection.getInputStream();
		FileOutputStream os = new FileOutputStream(filePath + "\\666.xlsx");
		byte[] buffer = new byte[4 * 1024];
		int read;
		while ((read = in.read(buffer)) > 0) {
			os.write(buffer, 0, read);
		}
		os.close();
		in.close();

		String filename = null;
		String agent = request.getHeader("USER-AGENT");
		if (null != agent) {
			if (-1 != agent.indexOf("Firefox")) {// Firefox
				filename = "=?UTF-8?B?"
						+ (new String(org.apache.commons.codec.binary.Base64.encodeBase64(filePath.getBytes("UTF-8"))))
						+ "?=";
			} else if (-1 != agent.indexOf("Chrome")) {// Chrome
				filename = new String(filePath.getBytes(), "ISO8859-1");
			} else {// IE7+
				filename = java.net.URLEncoder.encode(filePath, "UTF-8");
				filename = StringUtils.replace(filename, "+", "%20");// 替换空格
			}
		} else {
			filename = filePath;
		}
	}

	public void downtest(String path, HttpServletResponse response) throws IOException {
		File file = new File(path);
		long size = file.length();
		// 为了解决中文名称乱码问题 这里是设置文件下载后的名称
		String fileName = new String("000.xlsl".getBytes("UTF-8"), "iso-8859-1");
		response.reset();
		response.setHeader("Accept-Ranges", "bytes");
		// 设置文件下载是以附件的形式下载
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
		response.addHeader("Content-Length", String.valueOf(size));

		ServletOutputStream sos = response.getOutputStream();
		FileInputStream in = new FileInputStream(file);
		BufferedOutputStream outputStream = new BufferedOutputStream(sos);
		byte[] b = new byte[1024];
		int i = 0;
		while ((i = in.read(b)) > 0) {
			outputStream.write(b, 0, i);
		}
		outputStream.flush();
		sos.close();
		outputStream.close();
		in.close();
	}

}
